<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="渠道" prop="channelId">
        <el-input
          v-model="queryParams.channelId"
          placeholder="请输入渠道"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务" prop="serviceTypeId">
        <el-input
          v-model="queryParams.serviceTypeId"
          placeholder="请输入业务"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指令" prop="cmd">
        <el-input
          v-model="queryParams.cmd"
          placeholder="请输入指令"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="端口" prop="port">
        <el-input
          v-model="queryParams.port"
          placeholder="请输入端口"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扣量比例" prop="deduct">
        <el-input
          v-model="queryParams.deduct"
          placeholder="请输入扣量比例"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计费数" prop="billingCount">
        <el-input
          v-model="queryParams.billingCount"
          placeholder="请输入计费数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="所有" value="" />
          <el-option label="开通" value="0" />
          <el-option label="关闭" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="优先级" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入优先级"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sms:cmd:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sms:cmd:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sms:cmd:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sms:cmd:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cmdList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="优先级" align="center" prop="id" />
      <el-table-column label="渠道" align="center" prop="channelId" />
      <el-table-column label="业务" align="center" prop="serviceTypeId" />
      <el-table-column label="指令" align="center" prop="cmd" />
      <el-table-column label="端口" align="center" prop="port" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="扣量比例" align="center" prop="deduct" />
      <el-table-column label="计费数" align="center" prop="billingCount" />
      <el-table-column label="状态" align="center" prop="status" :formatter="formatterState"/>
      <el-table-column label="优先级" align="center" prop="weight" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sms:cmd:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sms:cmd:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改指令列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
           <el-form-item label="归属渠道" prop="channelId">
                        <treeselect v-model="form.channelId" :options="channelOptions" :show-count="false" placeholder="请选择归属渠道" />
                      </el-form-item>
        </el-form-item>
         <el-form-item label="归属业务" prop="serviceTypeId">
                            <treeselect v-model="form.serviceTypeId" :options="serviceTypeOptions" :show-count="false" placeholder="归属业务" />
                          </el-form-item>
        </el-form-item>
        <el-form-item label="指令" prop="cmd">
          <el-input v-model="form.cmd" placeholder="请输入指令" />
        </el-form-item>
        <el-form-item label="端口" prop="port">
          <el-input v-model="form.port" placeholder="请输入端口" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="扣量比例" prop="deduct">
          <el-input v-model="form.deduct" placeholder="请输入扣量比例" />
        </el-form-item>
        <el-form-item label="计费数" prop="billingCount">
          <el-input v-model="form.billingCount" placeholder="请输入计费数" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status" >
            <el-radio :label="0">开通</el-radio>
            <el-radio :label="1">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="优先级" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入优先级" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCmd, getCmd, delCmd, addCmd, updateCmd, exportCmd } from "@/api/sms/cmd";
import { treeselect } from "@/api/sms/channel";
import { serviceTypeTree } from "@/api/sms/serviceType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Cmd",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 指令列表表格数据
      cmdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,   // 部门树选项
      channelOptions: undefined,
      serviceTypeOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        channelId: null,
        serviceTypeId: null,
        cmd: null,
        port: null,
        price: null,
        deduct: null,
        billingCount: null,
        status: null,
        weight: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        channelId: [
          { required: true, message: "渠道不能为空", trigger: "blur" }
        ],
        serviceTypeId: [
          { required: true, message: "业务不能为空", trigger: "blur" }
        ],
        cmd: [
          { required: true, message: "指令不能为空", trigger: "blur" }
        ],
        port: [
          { required: true, message: "端口不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        deduct: [
          { required: true, message: "扣量比例不能为空", trigger: "blur" }
        ],
        billingCount: [
          { required: true, message: "计费数不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        weight: [
          { required: true, message: "优先级不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatterState(row, column){
      return row.status!=0?"已关闭":"已开通"
    },
    /** 查询指令列表列表 */
    getList() {
      this.loading = true;
      listCmd(this.queryParams).then(response => {
        this.cmdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询渠道业务下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.channelOptions = response.data;
        //this.serviceTypeOptions = response.data;
      });
    },
    getServiceTypeTree() {
      serviceTypeTree().then(response => {
        this.serviceTypeOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        channelId: null,
        serviceTypeId: null,
        cmd: null,
        port: null,
        price: null,
        deduct: null,
        billingCount: 0,
        status: 1,
        weight: null,
        createTime: null,
        updateTime: null,
        channelOptions: null,
        serviceTypeOptions: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
          this.open = true;
          this.title = "添加指令";
          this.getTreeselect();
          this.getServiceTypeTree();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCmd(id).then(response => {
        this.getTreeselect();
        this.getServiceTypeTree();
        this.form = response.data;
        this.open = true;
        this.title = "修改指令";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCmd(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCmd(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除指令列表编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCmd(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有指令列表数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCmd(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
